package com.zhny.project.txt.dataset.service.impl;

import com.zhny.common.utils.DateUtils;
import com.zhny.common.utils.security.ShiroUtils;
import com.zhny.common.utils.text.Convert;
import com.zhny.project.system.config.service.IConfigService;
import com.zhny.project.txt.dataset.domain.TxtDataset;
import com.zhny.project.txt.dataset.mapper.TxtDatasetMapper;
import com.zhny.project.txt.dataset.service.ITxtDatasetService;
import com.zhny.project.txt.history.domain.TxtHistory;
import com.zhny.project.txt.history.mapper.TxtHistoryMapper;
import com.zhny.project.txt.indexset.domain.TxtIndexset;
import com.zhny.project.txt.indexset.mapper.TxtIndexsetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * TXT数据集Service业务层处理
 *
 * @author yanqun
 * @date 2019-12-09
 */
@Service
public class TxtDatasetServiceImpl implements ITxtDatasetService
{
    @Autowired
    private TxtDatasetMapper txtDatasetMapper;
    @Autowired
    private TxtIndexsetMapper txtIndexsetMapper;
    @Autowired
    private IConfigService configService;
    @Autowired
    private TxtHistoryMapper txtHistoryMapper;

    /**
     * 查询TXT数据集
     *
     * @param datasetKey TXT数据集ID
     * @return TXT数据集
     */
    @Override
    public TxtDataset selectTxtDatasetById(String datasetKey)
    {
        return txtDatasetMapper.selectTxtDatasetById(datasetKey);
    }

    /**
     * 查询TXT数据集列表
     *
     * @param txtDataset TXT数据集
     * @return TXT数据集
     */
    @Override
    public List<TxtDataset> selectTxtDatasetList(TxtDataset txtDataset)
    {
        return txtDatasetMapper.selectTxtDatasetList(txtDataset);
    }

    /**
     * 新增TXT数据集
     *
     * @param txtDataset TXT数据集
     * @return 结果
     */
    @Override
    @Transactional
    public String insertTxtDataset(TxtDataset txtDataset)
    {
        String   table1 = configService.selectConfigByKey("txt_control_table");

        txtDataset.setDatasetKey(UUID.randomUUID().toString());
        txtDataset.setCreateTime(DateUtils.getNowDate());
        txtDataset.setSurfaceName(table1 + txtDataset.getDatasetId());
        txtDataset.setDisposeIcon("0");
        txtDataset.setDeleteFlag(0);
        txtDataset.setAnalystBy(ShiroUtils.getUserId().toString());
       int list =  txtDatasetMapper.insertTxtDataset(txtDataset);
       String string = txtDataset.getDatasetKey();
        return string;
    }

    /**
     * 修改TXT数据集
     *
     * @param txtDataset TXT数据集
     * @return 结果
     */
    @Override
    public int updateTxtDataset(TxtDataset txtDataset)
    {
        txtDataset.setUpdateTime(DateUtils.getNowDate());
        return txtDatasetMapper.updateTxtDataset(txtDataset);
    }

    /**
     * 删除TXT数据集对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTxtDatasetByIds(String ids)
    {
        return txtDatasetMapper.deleteTxtDatasetByIds(Convert.toStrArray(ids));
    }


    //创建数据表
    @Override
    @Transactional
    public String addsurface(String complete, TxtDataset txtDataset)
    {
        String   table1 = configService.selectConfigByKey("txt_control_table");
        String field = configService.selectConfigByKey("sys_control_field");
//        String features = configService.selectConfigByKey("sys_control_features");
//        System.out.print(features);
        List<String> list = new ArrayList<>();
        if (complete.contains(" ")){
            list = Arrays.asList(complete.split(" "));
        }
        if (complete.contains("\t")){
            list = Arrays.asList(complete.split("\t"));
        }
        if (complete.contains(",")){
            list = Arrays.asList(complete.split(","));
        }
        int mun = list.size();
        String target = "";


        //重新定义字段名称
        List<String> list1 = new ArrayList<>();
        for (int i=0; i<mun; i++){
             target = field + i;
            list1.add(target);
        }
        String target1 = "";
        List<String> list2 = new ArrayList<>();
        for (int i=0; i<mun; i++){
            target1 = field + i;
            String beginMonth = target1.substring(target1.indexOf("_"));
            String str =  beginMonth.toString().substring(1);
            System.out.print(str);
            list2.add(str + "_");
        }


        StringBuffer Sar = new StringBuffer();
        for (String s : list1) {
            Sar.append(s + ",");
        }
        //for循环变例字段

        List<String> myList=new ArrayList<String>();
        List<String> mysql=new ArrayList<String>();
        for (int j=0; j<mun; j++){
            String string = " `" + list1.get(j).trim() +"` FLOAT NULL,";
            myList.add(string);
        }

        for (int j=0; j<mun; j++){
            String string = " `" + list2.get(j).trim() +"quality` FLOAT NULL,";
            mysql.add(string);
        }
        //数组转换字符串
        StringBuffer str5 = new StringBuffer();
        for (String s : myList) {
            str5.append(s);
        }
        StringBuffer str6 = new StringBuffer();
        for (String s : mysql) {
            str6.append(s);
        }
        String all =  str5.toString().substring(2);
        String web =  str6.toString().substring(2);
        String tableID =  txtDataset.getDatasetId();
        StringBuffer sql = new StringBuffer();
        sql.append("CREATE TABLE `zhny`.`" + table1 + "" + tableID + "` (");
        sql.append(" `table_key` VARCHAR(36) NOT NULL,`");
        sql.append("dataset_key` VARCHAR(36) NULL,`");
        sql.append("delete_flag` VARCHAR(2) NULL,`");
        sql.append( all);
        sql.append( "`" + web);
        sql.append( "" + "`eigan_value` VARCHAR(36) NULL,");
        sql.append(" PRIMARY KEY (`table_key`))");
        List<Map<String, Object>> Result = txtDatasetMapper.establishtable(sql.toString());

        return Sar.toString();
    }

    //向数据表中添加数据
    @Override
    @Transactional
    public int insertTxtinto(String complete, TxtDataset txtDataset, List<String> surface, String deta){
//        txtDataset form表单中元素
//        surface 处理完毕字段名称
//        complete  导入字段名称

        String   table1 = configService.selectConfigByKey("txt_control_table");
        //新增代码
        List<String> list = new ArrayList<>();
        if (complete.contains(" ")){
            list = Arrays.asList(complete.split(" "));
        }
        if (complete.contains("\t")){
            list = Arrays.asList(complete.split("\t"));
        }
        if (complete.contains(",")){
            list = Arrays.asList(complete.split(","));
        }
        StringBuffer string = new StringBuffer();
            for (int i=0;i<list.size();i++){
                string.append(list.get(i).trim() + ",");
            }
        String   sis = string.substring(0,string.length() - 1);
        //一下原来代码
        StringBuffer stran = new StringBuffer();
        for (String s : surface) {
            stran.append(s);
        }
        String   text = stran.substring(0,stran.length() - 1);
        String table =  txtDataset.getDatasetId();
        txtDataset.setDatasetKey(UUID.randomUUID().toString());
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO " + table1 + "" + table );
        sql.append(" ( table_key,dataset_key,delete_flag," +  text +")VALUES( '" + txtDataset.getDatasetKey() );
        sql.append("','" + deta);
        sql.append("','0");
        sql.append("'," + sis + ");");

        List<Map<String, Object>> Result = txtDatasetMapper.insertTxtinto(sql.toString());

        return 1;
    }

    public int csvTxtinto(String complete, TxtDataset txtDataset, List<String> surface, String deta){
        String   table1 = configService.selectConfigByKey("txt_control_table");
        List<String> list = Arrays.asList(complete.split(","));
        StringBuffer string = new StringBuffer();
        for (int i=0;i<list.size();i++){
            string.append(list.get(i) + ",");
        }
        String   sis = string.substring(0,string.length() - 1);
        //一下原来代码
        StringBuffer stran = new StringBuffer();
        for (String s : surface) {
            stran.append(s);
        }
        String   text = stran.substring(0,stran.length() - 1);
        System.out.print(text);
        String table =  txtDataset.getDatasetId();
        txtDataset.setDatasetKey(UUID.randomUUID().toString());
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO " + table1 + "" + table );
        sql.append(" ( table_key,dataset_key,delete_flag," +  text +")VALUES( '" + txtDataset.getDatasetKey() );
        sql.append("','" + deta);
        sql.append("','0");
        sql.append("'," + sis + ");");
        System.out.print(sql.toString());
        System.out.print(sql);
        List<Map<String, Object>> Result = txtDatasetMapper.insertTxtinto(sql.toString());
        System.out.print(Result);
        return 1;

    }

    /**
     * 删除TXT数据集信息
     *
     * @param datasetKey TXT数据集ID
     * @return 结果
     */
    @Transactional
    public int deleteTxtDatasetById(String datasetKey)
    {
        int dele = 0;
        String   table1 = configService.selectConfigByKey("txt_control_table");

        //插入记录
        List<TxtIndexset> txtIndexsetList =  txtIndexsetMapper.seleindexsetKey(datasetKey);
        TxtHistory txtHistory = new TxtHistory();
        for (TxtIndexset txtIndexset : txtIndexsetList){
            txtHistory.setHistoryKey(UUID.randomUUID().toString());
            txtHistory.setDatasetKey(datasetKey);
            txtHistory.setIndexcollectKey(txtIndexset.getIndexsetKey());
            txtHistory.setDisposeType("1");
            txtHistory.setHistoryBy(ShiroUtils.getUserId().toString());
            txtHistoryMapper.insertTxtHistory(txtHistory);
        }



        //正式删除
        TxtDataset txtDataset = txtDatasetMapper.seleid(datasetKey);
        StringBuffer sql = new StringBuffer();
        sql.append("update " + table1 +"");
        sql.append(txtDataset.getDatasetId());
        sql.append(" set delete_flag=1 where dataset_key =");
        sql.append("'" + datasetKey + "'");
        System.out.print(sql);
        int Result = txtDatasetMapper.deletable(sql.toString());
        System.out.print(Result);
        if (txtIndexsetMapper.deleteTxtIndexsetById(datasetKey)>0){
            dele = 1;
        }else {
            dele = 0;
        }
        if (txtDatasetMapper.deleteTxtDatasetById(datasetKey)>0){
            dele = 1;
        }else {
            dele = 0;
        }



        return dele;
    }

    public int seletxtDataset( String datasetKey){
        return txtDatasetMapper.seletxtDataset(datasetKey);
    }


    //创建数据表
    @Override
    @Transactional
    public String csvsurface(String line, TxtDataset txtDataset)
    {
        String   table1 = configService.selectConfigByKey("txt_control_table");
        String field = configService.selectConfigByKey("sys_control_field");

        List<String> list = Arrays.asList(line.split(","));
        int mun = list.size();
        String target = "";

        //重新定义字段名称
        List<String> list1 = new ArrayList<>();
        for (int i=0; i<mun; i++){
            target = field + i;

            list1.add(target);
        }

        String target1 = "";
        List<String> list2 = new ArrayList<>();
        for (int i=0; i<mun; i++){
            target1 = field + i;
            String beginMonth = target1.substring(target1.indexOf("_"));
            String str =  beginMonth.toString().substring(1);
            System.out.print(str);
            list2.add(str + "_");
        }
        StringBuffer Sar = new StringBuffer();
        for (String s : list1) {
            Sar.append(s + ",");
        }
        //for循环变例字段

        List<String> myList=new ArrayList<String>();
        List<String> mysql=new ArrayList<String>();
        for (int j=0; j<mun; j++){
            String string = " `" + list1.get(j) +"` FLOAT(36) NULL,";
            myList.add(string);
        }

        for (int j=0; j<mun; j++){
            String string = " `" + list2.get(j) +"quality` FLOAT(36) NULL,";
            mysql.add(string);
        }
        System.out.print(myList);
        //数组转换字符串
        StringBuffer str5 = new StringBuffer();
        for (String s : myList) {
            str5.append(s);
        }
        StringBuffer str6 = new StringBuffer();
        for (String s : mysql) {
            str6.append(s);
        }
        String all =  str5.toString().substring(2);
        String web =  str6.toString().substring(2);
        System.out.println(str5.toString());
        System.out.print(list1);
        String tableID =  txtDataset.getDatasetId();
        StringBuffer sql = new StringBuffer();
        sql.append("CREATE TABLE `zhny`.`" + table1 + "" + tableID + "` (");
        sql.append(" `table_key` VARCHAR(36) NOT NULL,`");
        sql.append("dataset_key` VARCHAR(36) NULL,`");
        sql.append("delete_flag` VARCHAR(36) NULL,`");
        sql.append( all);
        sql.append( "`" + web);
        sql.append( "" + "`eigan_value` VARCHAR(36) NULL,");
        sql.append(" PRIMARY KEY (`table_key`))");
        System.out.print(sql);
        List<Map<String, Object>> Result = txtDatasetMapper.establishtable(sql.toString());
        System.out.print(Result);
        return Sar.toString();
    }
}
